package com.cicc.beergamevirtualsimulationsystem.validation;



import com.cicc.beergamevirtualsimulationsystem.annotation.EnumValid;
import com.cicc.beergamevirtualsimulationsystem.common.enums.BaseEnum;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @author EDY
 */
@Slf4j
public class EnumValidation implements ConstraintValidator<EnumValid,Integer> {

    private Class<? extends BaseEnum> enumClass;
    /**
     *
     * @param value 要校验的数据
     * @return false 则校验不通过 true 则通过
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        log.debug("开始自定义校验:{}",value);
        if(value == null){
            return false;
        }
        return BaseEnum.match(enumClass,value);
    }

    /**
     * 获取注解上的参数
     */
    @Override
    public void initialize(EnumValid constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        this.enumClass = constraintAnnotation.enumClass();
    }
}
